package top.ninwoo.bishe.starter.entity;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import top.ninwoo.common.entity.ContainerInfo;
import top.ninwoo.common.entity.ContainerMonitorInfo;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ContainerMonitor {

    private static final String GET_CONTAINER_INFO_BY_CONTAINER_ID
            = "/getContainerMonitorByContainerId?containerId={containerId}";
    // 远程edgeNode的ID
    private String edgeNodeId;
    // 对应cluster的id
    private long clusterId;
    // 对应的app name
    private String appName;

    private String monitorUrl;

    private String containerId;
    
    private RestTemplate restTemplate;


    public ContainerMonitor(ContainerMonitorInfo containerMonitorInfo, RestTemplate restTemplate) {
        this.edgeNodeId = containerMonitorInfo.getEdgeNodeId();
        this.clusterId = containerMonitorInfo.getClusterId();
        this.appName = containerMonitorInfo.getAppName();
        this.containerId = containerMonitorInfo.getContainerId();
        monitorUrl = "http://" + edgeNodeId + GET_CONTAINER_INFO_BY_CONTAINER_ID;
        this.restTemplate = restTemplate;
    }
    
    public ContainerInfo getContainerInfo() {
        Map<String, Object> param = new HashMap<>();
        param.put("containerId", this.containerId);
        ContainerInfo containerInfo
                = restTemplate.getForObject(monitorUrl, ContainerInfo.class, param);
        // 这里可能返回一个null的类，此时需要上级的调用函数做处理
        return containerInfo;
    }
}